<?php

class Ajax extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('schools');
		$this->load->model('states');
		$this->load->model('users');
	}
	
	public function colleges($state_id)
	{
		if (!Ajax::isAjaxRequest()) return;
		$data['schools'] = $this->schools->select_by_state_id($state_id, 'college');
		$this->load->view('ajax/schools', $data);
	}
	
	public function high_schools($state_id)
	{
		if (!Ajax::isAjaxRequest()) return;
		$data['schools'] = $this->schools->select_by_state_id($state_id, 'high school');
		$this->load->view('ajax/schools', $data);
	}
	
	public function states()
	{
		if (!Ajax::isAjaxRequest()) return;
		$data['states'] = $this->states->select_all();
		$this->load->view('ajax/states', $data);
	}
	
	public function users($role_id = 0, $school_id = 0)
	{
		if (!Ajax::isAjaxRequest()) return;
		$data['users'] = $this->users->select_by_school_id($school_id, $role_id);
		$this->load->view('ajax/users', $data);
	}
	
	private static function isAjaxRequest()
	{
		return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
	}
}